package com.js.rtu.mvp.present;


import android.util.Log;

import com.js.rtu.base.BasePresenter;
import com.js.rtu.fragment.third.DebugReadBean;
import com.js.rtu.mvp.bean.Cmd90Bean;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.util.BleUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

/**
 * 92查询所有参数数据解析
 * Created by wz on 2019/5/17.
 */
public class Cmd90Presenter implements BasePresenter<Cmd90Bean> {

    int index;

    @Override
    public CommonBean<Cmd90Bean> paseData(String msg) {
        Log.e("xxxx", msg);
        CommonBean<Cmd90Bean> bean = new CommonBean<>();
        Cmd90Bean cmd90Bean = new Cmd90Bean();
        index = 0;
        String[] data = msg.split(" ");


        if (getDataByIndex(data, 0).equals("EE") && getDataByIndex(data, 1).equals("EE")
                && getDataByIndex(data, data.length - 1).equals("FF")) {

            //帧头、CRC、帧尾
            bean.setStart(getDataByIndex(data, 0) + getDataByIndex(data, 1));
            bean.setCRC(getDataByIndex(data, data.length - 4) + getDataByIndex(data, data.length - 3));
            bean.setEnd(getDataByIndex(data, data.length - 1) + getDataByIndex(data, data.length - 2));
            index = 2;//2字节

            //address
            StringBuilder str_address = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                str_address.append(getDataByIndex(data, index + i));
            }
            index += 8;//8字节
            String address = BleUtils.hexStr2Str(str_address.toString());
            bean.setAddress(address);

            //length固定长度2，hex
            int length = BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index));
            index += 2;//2字节
            bean.setLength(length);

            //cmd固定长度1，hex
            int cmd = Integer.parseInt(getDataByIndex(data, index));
            index += 1;//1字节
            bean.setCmd(cmd);
            boolean isBreak = false;
            //这里开始就是data数据
            while (index < data.length - 5 && !isBreak) {
                //识别的字段
                String start = getDataByIndex(data, index++).toUpperCase();

                int currentData;
                switch (start) {
                    case "0E":
                        String runTime = "";
                        //终端开机后连续运行时间,共 4 字节,第 1-4 字节为时间，分别是天（2 字节，低在前），时、分
                        currentData = BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index));
                        runTime = runTime + currentData + "天";
                        index += 2;
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        runTime = runTime + currentData + "时";
                        index += 1;
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        runTime = runTime + currentData + "分";
                        index += 1;
                        cmd90Bean.setStr_0E(runTime);
                        break;
                    case "0F":
                        //终端无线信号强度, 5 字节；/第 1 字节是信号强度；后四字节是终端的电池电压（浮点数）
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        cmd90Bean.setStr_0F_signal(currentData + "");
                        index += 1;
//                        cmd90Bean.setStr_0F_Power( String.valueOf(BleUtils.hToint(getDataByIndex(data, index+3) + getDataByIndex(data, index+2) + getDataByIndex(data, index+1) + getDataByIndex(data, index))));

                        cmd90Bean.setStr_0F_Power(BleUtils.byte2Float(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2)
                                + getDataByIndex(data, index + 1) + getDataByIndex(data, index)));

                        index += 4;
                        break;
                    case "10":
                        //总发送帧,4字节
                        cmd90Bean.setStr_10H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "11":
                        //总接收帧,4字节
                        cmd90Bean.setStr_11H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "12":
                        //唤醒次数,4字节
                        cmd90Bean.setStr_12H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "13":
                        //总注册次数,4字节
                        cmd90Bean.setStr_13H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "14":
                        //总报警次数,4字节
                        cmd90Bean.setStr_14H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "15":
                        //总报警次数,4字节
                        cmd90Bean.setStr_15H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "16":
                        //程序版本,8 字节 ASCii 码，不足 8 位后面补 0x00V1.6 添
                        StringBuilder strVersion = new StringBuilder();
                        for (int i = 0; i < 8; i++) {
                            strVersion.append(getDataByIndex(data, index + i));
                        }
                        cmd90Bean.setStr_16H(BleUtils.hexStr2Str(strVersion.toString()));
                        index += 8;
                        break;
                    case "17":
                        //总报警次数,4字节
                        cmd90Bean.setStr_17H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "18":
                        //产品类型,16 字节 ASCII 码，不足 16 位后面补 0x00
                        StringBuilder strType = new StringBuilder();
                        for (int i = 0; i < 16; i++) {
                            strType.append(getDataByIndex(data, index + i));
                        }
                        cmd90Bean.setStr_18H(BleUtils.hexStr2Str(strType.toString()));
                        index += 16;
                        break;
                    case "19":
                        //终端开机后总注册次数,4字节
                        cmd90Bean.setStr_19H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "20":
                        //终端开机后总唤醒次数,4字节
                        cmd90Bean.setStr_20H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "21":
                        //终端开机后总报警次数,4字节
                        cmd90Bean.setStr_21H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "22":
                        //终端开机后发送数据包数,4字节
                        cmd90Bean.setStr_22H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "23":
                        //终端开机后接收数据包数,4字节
                        cmd90Bean.setStr_23H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "24":
                        //终端开机后总唤醒及成功自报次数,4字节
                        cmd90Bean.setStr_24H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "25":
                        //开机次数,4字节
                        cmd90Bean.setStr_25H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;
                    case "26":
                        //上次自报时间
                        String str_time = "20" +
                                BleUtils.hToint(getDataByIndex(data, index)) + "/" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 1))) +
                                "/" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 2))) +
                                " " +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 3))) +
                                ":" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 4))) +
                                ":" +
                                String.format(Locale.CHINA, "%02d",
                                        BleUtils.hToint(getDataByIndex(data, index + 5)));
                        cmd90Bean.setStr_26H(str_time);
                        index += 6;
                        break;

                    case "27":
                        //通信协议版本,2字节
                        cmd90Bean.setStr_27H(BleUtils.hToint(getDataByIndex(data, index + 1) + getDataByIndex(data, index)) + "");
                        index += 2;
                        break;
                    case "28":
                        //总唤醒及成功自报次数,4字节
                        cmd90Bean.setStr_28H(String.valueOf(BleUtils.hToint(getDataByIndex(data, index + 3) + getDataByIndex(data, index + 2) + getDataByIndex(data, index + 1) + getDataByIndex(data, index))));
                        index += 4;
                        break;

                    case "29":
                        //MAC,6字节
//                        cmd90Bean.setStr_29H( String.valueOf(BleUtils.hToint(getDataByIndex(data, index+5)+getDataByIndex(data, index+4)+getDataByIndex(data, index+3) + getDataByIndex(data, index+2) + getDataByIndex(data, index+1) + getDataByIndex(data, index))));
                        StringBuilder macStr = new StringBuilder();
                        for (int i = 0; i < 6; i++) {
                            macStr.append(getDataByIndex(data, index)).append(":");
                            index += 1;
                        }
                        if (macStr.length() > 0) {
                            macStr.deleteCharAt(macStr.length() - 1);
                        }
                        cmd90Bean.setStr_29H(macStr.toString());

//                        index += 6;

                        break;
                    case "30":
                        //国际移动设备识别码 IMEI,8字节,倒序
                        StringBuilder codeStr = new StringBuilder();
                        for (int i = 8; i >0 ; i--) {
                            codeStr.append(getDataByIndex(data, index+i-1));
                        }
                        index += 8;
                        cmd90Bean.setStr_30H(String.valueOf(Long.parseLong(codeStr.toString(),16)));

                        break;
                    case "31":
                        //国际移动用户识别码 IMSI  8字节
                        StringBuilder strNumber = new StringBuilder();
                        for (int i = 0; i < 8; i++) {
                            strNumber.append(getDataByIndex(data, index));
                            index += 1;
                        }
                        cmd90Bean.setStr_31H(strNumber.toString());

                        break;
                    case "32":
                        //终端出厂编号 8字节.倒序
                        StringBuilder idStr = new StringBuilder();
                        for (int i = 8; i >0 ; i--) {
                            idStr.append(getDataByIndex(data, index+i-1));
                        }
                        index += 8;
                        cmd90Bean.setStr_32H(String.valueOf(Long.parseLong(idStr.toString(),16)));


                        break;
                    case "33":
                        //SIM 卡 ICCID 20字节
                        StringBuilder iccid = new StringBuilder();
                        for (int i = 0; i < 20; i++) {
                            iccid.append(getDataByIndex(data, index));
                            index += 1;
                        }
                        cmd90Bean.setStr_33H(iccid.toString());
                        break;
                    case "34":
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        if (currentData == 170) {
                            cmd90Bean.setStr_34H("在线模式");
                        } else if (currentData == 85) {
                            cmd90Bean.setStr_34H("低功耗模式");
                        } else {
                            cmd90Bean.setStr_34H("未激活");
                        }
                        index += 1;
                        break;
                    case "35":
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        if (currentData == 170) {
                            cmd90Bean.setStr_35H("电池供电");
                        } else if (currentData == 85) {
                            cmd90Bean.setStr_35H("外电供电");
                        } else {
                            cmd90Bean.setStr_35H("未知");
                        }
                        index += 1;
                        break;
                    case "36":
                        currentData = BleUtils.hToint(getDataByIndex(data, index));
                        if (currentData == 170) {
                            cmd90Bean.setStr_34H("布防");
                        } else if (currentData == 85) {
                            cmd90Bean.setStr_34H("撤防");
                        } else {
                            cmd90Bean.setStr_34H("未初始化");
                        }
                        index = data.length - 1;
                        break;
                    case "FF":
                        //帧尾
                        isBreak = true;
                        break;
                    default:
                        //以上标志都没,标志移到最后，结束解析
                        index = data.length - 1;
                        isBreak = true;
                        break;
                }
            }

        }
        bean.setData(cmd90Bean);
        return bean;

    }

    private String getDataByIndex(String[] data, int index) {
        StringBuilder stringBuilder = new StringBuilder();
        if (index < data.length) {
            stringBuilder.append(data[index]).append(" ");
        } else {
            this.index = data.length - 1;
        }
        if (stringBuilder.length() > 0) {
            return stringBuilder.substring(0, stringBuilder.length() - 1);
        } else {
            return stringBuilder.toString();
        }
    }

    public void setInfo(CommonBean<Cmd90Bean> bean) {
        String line = "-----------------------------------------------------------\n";
        Cmd90Bean data = bean.getData();


        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append(String.format("出厂编码:%s\n", data.getStr_32H()))
                .append(String.format("蓝牙MAC:%s\n", data.getStr_29H()))
                .append(String.format("IMEI:%s\n", data.getStr_30H()))
                .append(String.format("终端开机后连续运行时间:%s\n", data.getStr_0E()))
                .append(String.format("信号强度:%s\n", data.getStr_0F_signal()))
                .append(String.format("电池电压:%s\n", data.getStr_0F_Power()))
                .append(String.format("程序版本:%s\n", data.getStr_16H()))
                .append(String.format("固件版本号:%s\n", data.getStr_17H()))
                .append(String.format("产品类型:%s\n", data.getStr_18H()))
                .append(String.format("通信协议版本:%s\n", data.getStr_27H()))
                .append(line);

        stringBuilder.append(String.format("%s\n", ">>>>>>>>终端运行状态<<<<<<<<"))
                .append(String.format("终端运行模式:%s\n", data.getStr_34H()))
                .append(String.format("终端电源状态:%s\n", data.getStr_35H()))
                .append(String.format("终端布防状态:%s\n", data.getStr_36H()))
                .append(line);


        stringBuilder.append(String.format("%s\n", ">>>>>>>>当前开机后状态<<<<<<<<"))
                .append(String.format("上次自报时间:%s\n", data.getStr_26H()))
                .append(String.format("注册次数:%s\n", data.getStr_19H()))
                .append(String.format("唤醒次数:%s\n", data.getStr_20H()))
                .append(String.format("总报警次数:%s\n", data.getStr_21H()))
                .append(String.format("发送数据包数:%s\n", data.getStr_22H()))
                .append(String.format("接收数据包数:%s\n", data.getStr_23H()))
                .append(String.format("唤醒及成功自报次数:%s\n", data.getStr_24H()))
                .append(line);


        stringBuilder.append(String.format("%s\n", ">>>>>>>>总运行状态<<<<<<<<"))
                .append(String.format("注册次数:%s\n", data.getStr_13H()))
                .append(String.format("唤醒次数:%s\n", data.getStr_12H()))
                .append(String.format("报警次数:%s\n", data.getStr_14H()))
                .append(String.format("发送数据包数:%s\n", data.getStr_10H()))
                .append(String.format("接收数据包数:%s\n", data.getStr_11H()))
                .append(String.format("唤醒及成功自报次数:%s\n", data.getStr_28H()))
                .append(String.format("总开机次数:%s\n", data.getStr_25H()))
                .append(String.format("总运行时间:%s\n", data.getStr_15H()))
                .append(line);

        EventBus.getDefault().post(new DebugReadBean(stringBuilder.toString()));
    }


}





